<%-- 
    Document   : admin
    Created on : Apr 5, 2011, 6:17:27 PM
    Author     : Yongke
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<script type="text/javascript"  language="Javascript">
var t;
function getIsi() {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
var url="admin-user.jsp";
self.xmlHttpReq.open('GET',url, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
document.getElementById("isi").innerHTML = self.xmlHttpReq.responseText;
}
}
self.xmlHttpReq.send(null);
}
</script>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="StyleSheet" href="admin.css" type="text/css" />
        <title>Admin Page</title>
    </head>
    <body onload="javascript:getIsi()">
        <%
        String uname = "null";
			if(session.getAttribute("username") != null)
                            uname = session.getAttribute("username").toString();

                        out.println("uname = " + uname);
        %>

        <div id="header"><img alt="header"  src="title.png" width="30%"></div>
        <a id="logout" href="index.jsp"><img src="logout.png" alt="logout" width="120px" /></a>
        <div id="isi">
        </div>
<%
  Connection con = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  Statement stmt = null;
  String username = request.getParameter("namauser");
  Integer id = 5;
  try {
    Class.forName("com.mysql.jdbc.Driver");
    con =DriverManager.getConnection 
    ("jdbc:mysql://localhost/myrc",
    "progin", "progin");
    ps = con.prepareStatement("update user set Role = 1 where Username = ?");
    ps.setString(1, username);
    ps.executeUpdate();
  } catch (ClassNotFoundException e) {
    throw new SQLException("JDBC Driver not found.", e);
  } finally {
    try {
      if(stmt != null){
        stmt.close();
        stmt = null;
      }
      if(ps != null) {
        ps.close();
        ps = null;
      }
      if(con != null) {
        con.close();
        con = null;
      }
    } catch (SQLException e) {}
  }
%>
<%
  Connection conM = null;
  PreparedStatement psM = null;
  ResultSet rsM = null;
  Statement stmtM = null;
  String namamod = request.getParameter("namamod");
  try {
    Class.forName("com.mysql.jdbc.Driver");
    conM =DriverManager.getConnection
    ("jdbc:mysql://localhost/myrc",
    "progin", "progin");
    psM = conM.prepareStatement("update user set Role = 0 where Username = ?");
    psM.setString(1, namamod);
    psM.executeUpdate();
  } catch (ClassNotFoundException e) {
    throw new SQLException("JDBC Driver not found.", e);
  } 
%>
<%
  String statusban1 = request.getParameter("statusban1");
  try {
    Class.forName("com.mysql.jdbc.Driver");
    conM =DriverManager.getConnection
    ("jdbc:mysql://localhost/myrc",
    "progin", "progin");
    psM = conM.prepareStatement("update user set StatusBan = 1 where Username = ?");
    psM.setString(1, statusban1);
    psM.executeUpdate();
  } catch (ClassNotFoundException e) {
    throw new SQLException("JDBC Driver not found.", e);
  }
String statusban2 = request.getParameter("statusban2");
  try {
    Class.forName("com.mysql.jdbc.Driver");
    conM =DriverManager.getConnection
    ("jdbc:mysql://localhost/myrc",
    "progin", "progin");
    psM = conM.prepareStatement("update user set StatusBan = 0 where Username = ?");
    psM.setString(1, statusban2);
    psM.executeUpdate();
  } catch (ClassNotFoundException e) {
    throw new SQLException("JDBC Driver not found.", e);
  }
   finally {
    try {
      if(stmtM != null){
        stmtM.close();
        stmtM = null;
      }
      if(psM != null) {
        psM.close();
        psM = null;
      }
      if(conM != null) {
        conM.close();
        conM = null;
      }
    } catch (SQLException e) {}
  }
%>
<%
        String hapuskat = request.getParameter("hapuskat");
        Connection conn = null;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection("jdbc:mysql://localhost/myrc","progin", "progin");
        PreparedStatement psDeleteRecord=null;
        PreparedStatement psDeleteRecord2=null;
        String sqlDeleteRecord=null;
        String sqlDeleteRecord2=null;
            sqlDeleteRecord="Delete from kategori where ID_Kategori=?";
            psDeleteRecord=conn.prepareStatement(sqlDeleteRecord);
            psDeleteRecord.setString(1, hapuskat);
            psDeleteRecord.executeUpdate();
            sqlDeleteRecord2="Delete from subkategori where ID_Kategori=?";
            psDeleteRecord2=conn.prepareStatement(sqlDeleteRecord2);
            psDeleteRecord2.setString(1, hapuskat);
            psDeleteRecord2.executeUpdate();
        String hapussub = request.getParameter("hapussub");
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection("jdbc:mysql://localhost/myrc","progin", "progin");
            sqlDeleteRecord="Delete from subkategori where ID_SubKategori=?";
            psDeleteRecord=conn.prepareStatement(sqlDeleteRecord);
            psDeleteRecord.setString(1, hapussub);
            psDeleteRecord.executeUpdate();
        try
        {
          if(psDeleteRecord!=null)
          {
           psDeleteRecord.close();
          }

          if(conn!=null)
          {
           conn.close();
          }
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
%>

<%
String kategori = request.getParameter("addkategori");
Connection connection = null;
PreparedStatement pstatement = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
int updateQuery = 0;
if(kategori!=null){
     if(kategori!="") {
            try {
              connection = DriverManager.getConnection("jdbc:mysql://localhost/myrc?user=progin&password=progin");
              String queryString = "INSERT INTO kategori(Nama_Kategori) VALUES (?)";
              pstatement = connection.prepareStatement(queryString);
              pstatement.setString(1, kategori);
              updateQuery = pstatement.executeUpdate();
            }
            catch (Exception ex) {
            out.println("Unable to connect to batabase.");

            }
            finally {
            }
}
}
%>
<%
String kategoriX = request.getParameter("addkategoriX");
String subkategori = request.getParameter("addsubkategori");

 Class.forName("com.mysql.jdbc.Driver").newInstance();
 if(subkategori!=null){
         if(subkategori!="") {
                try {
                  connection = DriverManager.getConnection("jdbc:mysql://localhost/myrc?user=progin&password=progin");
                  String queryString = "INSERT INTO subkategori(Nama_SubKategori,ID_Kategori) VALUES (?,?)";
                  pstatement = connection.prepareStatement(queryString);
                  pstatement.setString(1,subkategori);
                  pstatement.setString(2, kategoriX);
                  updateQuery = pstatement.executeUpdate();
                }
                catch (Exception ex) {
                out.println("Unable to connect to batabase.");

                }
                finally {
                }
  }
}
%>
<%
String roommaxX = request.getParameter("roommaxX");
String roommax = request.getParameter("roommax");

 Class.forName("com.mysql.jdbc.Driver").newInstance();
 if(roommax!=null){
         if(roommax!="") {
                try {
                  connection = DriverManager.getConnection("jdbc:mysql://localhost/myrc?user=progin&password=progin");
                  String queryString = "UPDATE user set NumRoom = ? WHERE Username = ?";
                  pstatement = connection.prepareStatement(queryString);
                  pstatement.setString(1, roommax);
                  pstatement.setString(2, roommaxX);
                  updateQuery = pstatement.executeUpdate();
                }
                catch (Exception ex) {
                out.println("Unable to connect to batabase.");

                }
                finally {
                }
  }
}
%>
<%
String idkat = request.getParameter("idkat");
String changenamekat = request.getParameter("changenamekat");

 Class.forName("com.mysql.jdbc.Driver").newInstance();
 if(changenamekat!=null){
         if(changenamekat!="") {
                try {
                  connection = DriverManager.getConnection("jdbc:mysql://localhost/myrc?user=progin&password=progin");
                  String queryString = "UPDATE kategori set Nama_Kategori = ? WHERE ID_Kategori= ?";
                  pstatement = connection.prepareStatement(queryString);
                  pstatement.setString(1, changenamekat);
                  pstatement.setString(2, idkat);
                  updateQuery = pstatement.executeUpdate();
                }
                catch (Exception ex) {
                out.println("Unable to connect to batabase.");

                }
                finally {
                }
  }
}
%>
<%
String idsub = request.getParameter("idsub");
String changenamesub= request.getParameter("changenamesub");

 Class.forName("com.mysql.jdbc.Driver").newInstance();
 if(changenamesub!=null){
         if(changenamesub!="") {
                try {
                  connection = DriverManager.getConnection("jdbc:mysql://localhost/myrc?user=progin&password=progin");
                  String queryString = "UPDATE subkategori set Nama_SubKategori = ? WHERE ID_SubKategori= ?";
                  pstatement = connection.prepareStatement(queryString);
                  pstatement.setString(1, changenamesub);
                  pstatement.setString(2, idsub);
                  updateQuery = pstatement.executeUpdate();
                }
                catch (Exception ex) {
                out.println("Unable to connect to batabase.");

                }
                finally {
                        pstatement.close();
                        connection.close();
                }
  }
}
%>

    </body>
</html>
